package com.vtb.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.base.VtbException;
import com.vtb.entity.QqDetailInfo;
import com.vtb.entity.QqOauth;
import com.vtb.inter.VtbDaoInterface;
import com.vtb.utils.JdbcUtils;
/**
 * QqDao实现类
 *
 * @author Chen Tao
 * @date 2012-7-1 下午04:53:33
 */
@SuppressWarnings("unchecked")
public class QqDaoImpl implements VtbDaoInterface<QqOauth, QqDetailInfo> {
	private static final Log log = LogFactory.getLog(QqDaoImpl.class);
	public void insert(QqOauth qqOauth) {}
    @Override
    public void del(QqOauth qqOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "delete from qqdata where uni=?";
            qr.update(JdbcUtils.getConnection(), sql, qqOauth.getUni());
        } catch (SQLException e) {
            log.error("删除qqdata错误：", e);
            throw new VtbException("删除qqdata错误"+e);
        }
        
    }
    @SuppressWarnings("deprecation")
    @Override
    public QqOauth get(QqOauth qqOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from qqdata where uni=?";
            qqOauth = (QqOauth) qr.query(JdbcUtils.getConnection(), sql,qqOauth.getUni(),new BeanHandler(QqOauth.class));
            return qqOauth;
        } catch (SQLException e) {
            log.error("通过qqoauth查找qqdata错误：", e);
            throw new VtbException("通过qqoauth查找qqdata错误"+e);
        }
    }
    @Override
    public List<QqOauth> getAll() {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from qqdata";
            return (List<QqOauth>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(QqOauth.class));
        } catch (SQLException e) {
            log.error("查询qqdata所有信息：", e);
            throw new VtbException("查询qqdata所有信息"+e);
        }
    }
    @Override
    public List<QqDetailInfo> getAllInfo() {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "select * from qqdata";
            return (List<QqDetailInfo>) qr.query(JdbcUtils.getConnection(), sql,new BeanListHandler(QqDetailInfo.class));
        } catch (SQLException e) {
            log.error("查询qqdata所有信息：", e);
            throw new VtbException("查询qqdata所有信息"+e);
        }
    }
    @Override
    public void insertAll(QqDetailInfo qqDetailInfo) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "insert into qqdata(uni,oauth_token,oauth_token_secrete,createTime,refr,refs,q2r,q2s,email,nick,location) values (?,?,?,?,?,?,?,?,?,?,?)";
            Object[] params = {qqDetailInfo.getUni(),qqDetailInfo.getOauth_token(),qqDetailInfo.getOauth_token_secrete(),qqDetailInfo.getCreateTime(),qqDetailInfo.getRefr(),qqDetailInfo.getRefs(),qqDetailInfo.getQ2r(),qqDetailInfo.getQ2s(),qqDetailInfo.getEmail(),qqDetailInfo.getNick(),qqDetailInfo.getLocation()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("插入qqdata错误：", e);
            throw new VtbException("插入qqdata错误",e);
        }
        
    }
    @Override
    public void update(QqOauth qqOauth) {
        try {
            QueryRunner qr = new QueryRunner();
            String sql = "update qqdata set oauth_token=?,oauth_token_secrete=?,createTime=?,refr=?,refs=?,q2r=?,q2s=? where uni=?";
            Object[] params = {qqOauth.getOauth_token(),qqOauth.getOauth_token_secrete(),qqOauth.getCreateTime(),qqOauth.getRefr(),qqOauth.getRefs(),qqOauth.getQ2r(),qqOauth.getQ2s(),qqOauth.getUni()};
            qr.update(JdbcUtils.getConnection(), sql, params);
        } catch (SQLException e) {
            log.error("更新qqdata错误：", e);
            throw new VtbException("更新qqdata错误"+e);
        }
        
    }

}
